package com.lagou.user.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lagou.entity.User;
import com.lagou.entity.UserDTO;
import com.lagou.user.UserService;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * @BelongsProject: lagou-edu-web
 * @Author: GuoAn.Sun
 * @CreateTime: 2020-09-08 11:22
 * @Description:
 */

@RestController
@RequestMapping("user")
public class UserController {

    @Reference // 远程消费
    private UserService userService;

    @GetMapping("login")
    public UserDTO login(String phone, String password,String nickname,String headimg) {
        UserDTO dto = new UserDTO();
        User user = null;
        System.out.println("phone = " + phone);
        System.out.println("password = " + password);
        System.out.println("nickname = " + nickname);

        // 检测手机号是否注册
        Integer i = userService.checkPhone(phone);
        if(i == 0){
            // 未注册，自动注册并登录
            userService.register(phone, password,nickname,headimg);
            dto.setMessage("手机号尚未注册，系统已帮您自动注册，请牢记密码！");
            user = userService.login(phone, password);
        }else{
            user = userService.login(phone, password);
            if(user == null){
                dto.setState(300); //300表示失败
                dto.setMessage("帐号密码不匹配，登录失败！");
            }else{
                dto.setState(200); //200表示成功
                dto.setMessage("登录成功！");
            }
        }
        dto.setContent(user);
        return dto;
    }

    @GetMapping("updateUserInfo")
    public UserDTO updateUserInfo(String userid, String portrait, String name) {
        UserDTO dto = new UserDTO();
        Integer i = userService.update(userid,portrait,name);
        if(i!=0)
        {
            dto.setState(200);
            dto.setMessage("修改成功");
        }
        else
        {
            dto.setState(300);
            dto.setMessage("更新信息失败");
        }
        User user=userService.selectbyid(userid);
        dto.setContent(user);
        return dto;
    }
    @GetMapping("updatepassword")
    public UserDTO updatepassword(String userid,String password)
    {
        UserDTO dto = new UserDTO();
        Integer i = userService.updatepasword(userid,password);
        if(i!=0)
        {
            dto.setState(200);
            dto.setMessage("修改密码成功");
        }
        else
        {
            dto.setState(300);
            dto.setMessage("更新密码失败");
        }
        User user=userService.selectbyid(userid);
        dto.setContent(user);
        return dto;
    }

    @PostMapping("uploadimg")
    public String uploadimg(@RequestParam("img") MultipartFile pic) throws Exception{
        String fileID=null;
        String url=pic.getOriginalFilename();
        System.out.println(url);
        try {
            // 加载配置文件
            ClientGlobal.initByProperties("E:\\java_study\\java_study_8_1\\lagou-edu-web\\src\\main\\resources\\conf\\fastdfs-client.properties");
            // 创建tracker客户端
            TrackerClient trackerClient = new TrackerClient();
            // 通过tracker客户端获取tracker的连接服务并返回
            TrackerServer trackerServer = trackerClient.getConnection();
            // 声明storage服务
            StorageServer storageServer = null;
            // 定义storage客户端
            StorageClient1 client = new StorageClient1(trackerServer, storageServer);
            // 定义文件元信息
            NameValuePair[] list = new NameValuePair[1];
            list[0] = new NameValuePair("fileName", "test.jpg");
            fileID = client.upload_file1(pic.getBytes(), "jpg", list);
            System.out.println("fileID = " + fileID);
            trackerServer.close();

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return fileID;
    }


    @GetMapping("updateUserInfoTest")
    public UserDTO updateUserInfoTest() {
        UserDTO userDTO=new UserDTO();
        try {
            // 加载配置文件
            ClientGlobal.initByProperties("E:\\java_study\\java_study_8_1\\lagou-edu-web\\src\\main\\resources\\conf\\fastdfs-client.properties");
            // 创建tracker客户端
            TrackerClient trackerClient = new TrackerClient();
            // 通过tracker客户端获取tracker的连接服务并返回
            TrackerServer trackerServer = trackerClient.getConnection();
            // 声明storage服务
            StorageServer storageServer = null;
            // 定义storage客户端
            StorageClient1 client = new StorageClient1(trackerServer, storageServer);
            // 定义文件元信息
            NameValuePair[] list = new NameValuePair[1];
            list[0] = new NameValuePair("fileName", "1.jpg");
            String fileID = client.upload_file1("E:\\java_study\\java_study_7_2\\timg.jpg", "jpg", list);
            System.out.println("fileID = " + fileID);
            //  UserController.updateUserInfo("123",fileID,"曹操");
            userDTO=updateUserInfo("100030025",fileID,"LauraLuo");
            trackerServer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userDTO;
    }

}

